<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\User;

use App\Controller\AppBaseController;
use App\Model\MultiCollect;
use App\Model\MultiLearn;
use App\Model\MultiZan;
use App\Model\Work;
use App\Model\WorkPush;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\WxApiAuth;

#[Controller(prefix: 'user')]
#[Middleware(WxApiAuth::class)]
class WorkController extends AppBaseController
{
    /**
     * 党务列表
     * @param int|null $party_id
     * @return object
     */
    #[RequestMapping(path:'workList[/{party_id}]',methods: 'get')]
    public function workList(int|null $party_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;


            $where['status'] = 1;
            $where['pass_status'] = 1;
            //默认获取自己支部信息
            if($party_id==null && is_numeric($party_id)){
                $where['party_id'] = $party_id;
            }

            $list = Work::with(['party'=>function($query){
                    $query->selectRaw('id,name')->get();
                }])->where($where)
                ->selectRaw('id,title,party_id,created_at')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();
            $next = 0;
            if($list->isNotEmpty()){
                if(count($list)>=$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list, 'next' => $next]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 党务推送列表
     * @return object
     */
    #[RequestMapping(path:'workPushList',methods: 'get')]
    public function workPushList(): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $dateTime = date('Y-m-d H:i:s',time());
            $where = [
                ['uid','=',$_user->id],
                ['push_time','<=',$dateTime]
            ];
            $list = WorkPush::with(['work'=>function($query){
                    $query->selectRaw('id,title,photo,created_at')->get();
                }])->where($where)
                ->selectRaw('id,work_id,uid')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();
            $next = 0;
            if($list->isNotEmpty()){
                if(count($list)>=$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list, 'next' => $next]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }



    /**
     * 党务详情
     * @param int $work_id
     * @return object
     */
    #[RequestMapping(path:'workInfo/{work_id}',methods: 'get')]
    public function workInfo(int $work_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $info = Work::with(['party'=>function($query){
                $query->selectRaw('id,name')->get();
            }])->where('id',$work_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);

            $info->is_collect = 0;
            $info->is_learn = 0;
            $info->is_zan = 0;
            //处理用户是否收藏
            $whereDo['module'] = 'work';
            $whereDo['relation_id'] = $info->id;

            //获取点赞数量
            $info->zan_count = MultiZan::where($whereDo)->count();

            $whereDo['uid'] = $_user->id;
            //判断是否学习
            $learnInfo = MultiLearn::where($whereDo)->first();
            if($learnInfo) $info->is_learn = 1;
            //判断是否收藏
            $whereDo['status'] = 1;
            $collectInfo = MultiCollect::where($whereDo)->first();
            if($collectInfo) $info->is_collect = 1;
            //判断是否点赞
            $zanInfo = MultiZan::where($whereDo)->first();
            if($zanInfo) $info->is_zan = 1;

            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }



    /**
     * 党务发布|编辑
     * @param int|null $work_id
     * @return object
     */
    #[RequestMapping(path: 'workEdit[/{work_id}]',methods: 'post,get')]
    public function workEdit(int|null $work_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            //判断权限
            if(!in_array($_user->user_type,[8,9])) return $this->reMsg->re(10009);

            $data = $this->request->all();
            //虚拟数据
//            if(empty($data)){
//                $data['party_id']  =1;
//                $data['uid'] = 1;
//                $data['title'] = '虚拟数据1';
//                $data['photo'] = 'https://www.baidu.com';
//                $data['content'] = '虚拟数据虚拟数据虚拟数据虚拟数据虚拟数据虚拟数据虚拟数据虚拟数据';
//                $data['media_list'] = [
//                    '0'=>'https://www.baidu.com',
//                    '1'=>'https://www.baidu.com',
//                    '2'=>'https://www.baidu.com'
//                ];
//                $data['uid'] = 1;
//                $data['status'] = 1;
//            }
            //虚拟数据

            if(empty($data['title'])) return $this->reMsg->re(10007);
            if(empty($data['photo'])) $data['photo'] = null;
            if(empty($data['content'])) $data['content'] = null;
            if(empty($data['media_list']) || !is_array($data['media_list'])) $data['media_list'] = [];
            if(empty($data['file_list']) || !is_array($data['file_list'])) $data['file_list'] = [];

            empty($data['status']) || !is_numeric($data['status']) ? $data['status'] = 0 : $data['status'] = 1;

            if($work_id){
                //编辑
                $info = Work::where('id',$work_id)->first();
                if(empty($info)) return $this->reMsg->re(10008);
                //判断是否有权操作
                if($_user->party_id!=$info->party_id) return $this->reMsg->re(10009);
                $info->uid = $_user->id;
                $info->title = $data['title'];
                $info->photo = $data['photo'];
                $info->content = $data['content'];
                $info->media_list = $data['media_list'];
                $info->status = $data['status'];
                $info->save();
                return $this->reMsg->re(1);
            }else{
                //发布
                $data['party_id'] = $_user->party_id;
                $data['uid'] = $_user->id;
                $insertId = Work::create($data);
                if($insertId){
                    return $this->reMsg->re(1);
                }else{
                    return $this->reMsg->re(0);
                }
            }
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 作品删除
     * @param int $work_id
     * @return object
     */
    #[RequestMapping(path:'workDelete/{work_id}',methods: 'get')]
    public function workDelete(int $work_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            //判断信息
            $info = Work::where('id',$work_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);
            //判断是否有权操作
            if($info->party_id!=$_user->party_id || !in_array($_user->user_type,[8,9])) return $this->reMsg->re(10009);
            $info->delete();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }




}